package com.burgeon.backend.EnterpriseWeChat.rest.Gtasks;

import com.burgeon.backend.EnterpriseWeChat.entity.Fitting;
import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;

public class GetFittingRate extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public GetFittingRate(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public Fitting getData(String empId) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(empId), url);
//        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");

        if (!"0".equals(code)) {
            throw new Exception(new JSONArray(str).getJSONObject(0).getString("message"));
        }
        String count = new JSONArray(str).getJSONObject(0).getString("count");
//        HashMap map = new HashMap();
        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");


        if(jsonArray.length()>0){
            Fitting fitting = new Fitting();
            DecimalFormat percent = new DecimalFormat("0.00");
            fitting.setToyQty(jsonArray.getJSONArray(0).getString(0));
            fitting.setReturnQty(jsonArray.getJSONArray(0).getString(1));
            fitting.setRetailQty(jsonArray.getJSONArray(0).getString(2));
            fitting.setNoReturnQty(jsonArray.getJSONArray(0).getString(3));
            fitting.setRate(percent.format(jsonArray.getJSONArray(0).getDouble(4)));
            return fitting;
        }


        return null;
    }

    private MultiValueMap Data(String empId) {
        HashMap params = new HashMap();
        ArrayList colums = new ArrayList();//查询字段
        colums.add("'TOT_QTY'");//总试衣数
        colums.add("'RETURN_QTY'");//总归还数
        colums.add("RETAIL_QTY");//总零售数量
        colums.add("NO_RETURN_QTY");//未归还数
        colums.add("RATE");//成交率

        params.put("columns", colums);
        params.put("table", "M_V_FITTINGRATE");//表名
        params.put("count", "true");

//        HashMap orderby = new HashMap();
//
//        orderby.put("column", "BILLDATE");
//        orderby.put("asc", "false");
//        ArrayList list = new ArrayList();
//        list.add(orderby);
//
//        params.put("orderby", list);

        HashMap paramss = new HashMap();
//        HashMap expr1 = new HashMap();
        paramss.put("column","'HR_EMPLOYEE_ID'");
        String ac = "'"+empId+"'";
        paramss.put("condition",ac);

//        HashMap expr2 = new HashMap();
//        expr2.put("column","BJ");
//        String state= null;
//        if(type.equals("1")){
//            state="'未回访'";
//        }else if(type.equals("2")){
//            state="'已回访'";
//        }
//
//        expr2.put("condition",state);
//
//        paramss.put("combine", "and");
//        paramss.put("expr1", expr1);
//        paramss.put("expr2", expr2);

        params.put("params", paramss);
//        System.err.println(paramss);
        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        GetFittingRate g = new GetFittingRate("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(g.getData("6037"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
